package com.lms.service.user;

import com.lms.dao.BaseDao;
import com.lms.dao.user.UserDao;
import com.lms.dao.user.UserDaoImpl;
import com.lms.bean.User;
import com.lms.util.Constants;
import org.apache.commons.codec.digest.DigestUtils;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private Connection connection = null;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {
        User user = null;

        connection = BaseDao.getConnection();
        user = userDao.getLoginUser(connection,userCode);
        //密码校对
        if (user != null && user.getUserPassWord().equals(DigestUtils.md5Hex(Constants.SALT_HEAD+password+ Constants.SALT_FOOT))) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public boolean updatePwd(String userCode, String pwd) {
        UserDao userDao = new UserDaoImpl();
        boolean flag = false;

        connection = BaseDao.getConnection();
        try {
            if (connection != null && userDao.updatePwd(connection, userCode, DigestUtils.md5Hex(Constants.SALT_HEAD+pwd+Constants.SALT_FOOT)) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public int getUserCount(String userName, int userRole) {
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        List<User> userList = null;

        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    @Override
    public boolean addUser(User newUser) {
        boolean flag = false;
        if (newUser != null) {
            try {
                connection = BaseDao.getConnection();
                connection.setAutoCommit(false);//开启JDBC事务
                flag = userDao.addUser(connection, newUser);
                connection.commit();
            } catch (Exception e) {
                try {
                    connection.rollback();
                } catch (SQLException throwables) {
                    System.out.println("添加失败！");
                    throwables.printStackTrace();
                    return false;
                }
                e.printStackTrace();
            } finally {
                BaseDao.closeResource(connection, null, null);
            }
        }
        return flag;
    }

    @Override
    public User getUserByCode(String userCode) {
        connection = BaseDao.getConnection();
        return userDao.getLoginUser(connection, userCode);
    }

    @Override
    public boolean modifyById(User user) {
        connection = BaseDao.getConnection();
        boolean flag = false;
        try {
            if (userDao.modifyById(connection, user) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    @Override
    public String getRootMsg() {
        String rootMsg="";
        connection=BaseDao.getConnection();
        try {
            rootMsg = userDao.getRootMsg(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return rootMsg;
    }
}
